package cn.edu.tju.elm.model;

import cn.edu.tju.core.model.BaseEntity;
import cn.edu.tju.core.model.User;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 银行卡实体类
 */
@Entity
@Table(name = "bank_cards")
public class BankCard extends BaseEntity {

    /**
     * 关联的用户
     */
    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    @NotNull
    private User user;

    /**
     * 银行卡号（加密存储）
     */
    @Column(name = "card_number", length = 50, nullable = false)
    @NotBlank
    @Size(max = 50)
    private String cardNumber;

    /**
     * 银行卡类型（1-储蓄卡，2-信用卡）
     */
    @Column(name = "card_type", nullable = false)
    @NotNull
    private Integer cardType;

    /**
     * 银行名称
     */
    @Column(name = "bank_name", length = 100, nullable = false)
    @NotBlank
    @Size(max = 100)
    private String bankName;

    /**
     * 持卡人姓名
     */
    @Column(name = "cardholder_name", length = 50, nullable = false)
    @NotBlank
    @Size(max = 50)
    private String cardholderName;

    /**
     * 手机号
     */
    @Column(name = "phone", length = 20)
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    private String phone;

    /**
     * 银行卡状态（1-正常，2-冻结，3-注销）
     */
    @Column(name = "status", nullable = false)
    @NotNull
    private Integer status = 1;

    /**
     * 余额（仅储蓄卡有效）
     */
    @Column(name = "balance", precision = 15, scale = 2)
    private BigDecimal balance = BigDecimal.ZERO;

    /**
     * 信用额度（仅信用卡有效）
     */
    @Column(name = "credit_limit", precision = 15, scale = 2)
    private BigDecimal creditLimit = BigDecimal.ZERO;

    /**
     * 已用额度（仅信用卡有效）
     */
    @Column(name = "used_credit", precision = 15, scale = 2)
    private BigDecimal usedCredit = BigDecimal.ZERO;

    /**
     * 绑定时间
     */
    @Column(name = "bind_time", nullable = false)
    @NotNull
    private LocalDateTime bindTime;

    /**
     * 最后使用时间
     */
    @Column(name = "last_used_time")
    private LocalDateTime lastUsedTime;

    /**
     * 是否默认支付卡
     */
    @Column(name = "is_default")
    private Boolean isDefault = false;

    /**
     * 备注
     */
    @Column(name = "remark", length = 500)
    @Size(max = 500)
    private String remark;

    // 构造函数
    public BankCard() {
        this.bindTime = LocalDateTime.now();
    }

    // Getter和Setter方法
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public Integer getCardType() {
        return cardType;
    }

    public void setCardType(Integer cardType) {
        this.cardType = cardType;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getCardholderName() {
        return cardholderName;
    }

    public void setCardholderName(String cardholderName) {
        this.cardholderName = cardholderName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public BigDecimal getCreditLimit() {
        return creditLimit;
    }

    public void setCreditLimit(BigDecimal creditLimit) {
        this.creditLimit = creditLimit;
    }

    public BigDecimal getUsedCredit() {
        return usedCredit;
    }

    public void setUsedCredit(BigDecimal usedCredit) {
        this.usedCredit = usedCredit;
    }

    public LocalDateTime getBindTime() {
        return bindTime;
    }

    public void setBindTime(LocalDateTime bindTime) {
        this.bindTime = bindTime;
    }

    public LocalDateTime getLastUsedTime() {
        return lastUsedTime;
    }

    public void setLastUsedTime(LocalDateTime lastUsedTime) {
        this.lastUsedTime = lastUsedTime;
    }

    public Boolean getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * 获取可用余额
     * 储蓄卡：返回余额
     * 信用卡：返回可用信用额度
     */
    public BigDecimal getAvailableAmount() {
        if (cardType == 1) { // 储蓄卡
            return balance != null ? balance : BigDecimal.ZERO;
        } else if (cardType == 2) { // 信用卡
            BigDecimal limit = creditLimit != null ? creditLimit : BigDecimal.ZERO;
            BigDecimal used = usedCredit != null ? usedCredit : BigDecimal.ZERO;
            return limit.subtract(used);
        }
        return BigDecimal.ZERO;
    }

    /**
     * 检查银行卡是否可用
     */
    public boolean isAvailable() {
        return status != null && status == 1;
    }

    @Override
    public String toString() {
        return "BankCard{" +
                "id=" + getId() +
                ", cardNumber='" + cardNumber + '\'' +
                ", cardType=" + cardType +
                ", bankName='" + bankName + '\'' +
                ", cardholderName='" + cardholderName + '\'' +
                ", status=" + status +
                ", balance=" + balance +
                ", bindTime=" + bindTime +
                ", isDefault=" + isDefault +
                '}';
    }
}

